package com.educoart.portlet;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import com.educoart.util.IConstants;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.search.Indexer;
import com.liferay.portal.kernel.search.IndexerRegistryUtil;
import com.liferay.portal.kernel.search.SearchException;
import com.liferay.portal.kernel.util.StringPool;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.portal.model.Contact;
import com.liferay.portal.model.ListType;
import com.liferay.portal.model.Organization;
import com.liferay.portal.model.User;
import com.liferay.portal.service.ListTypeServiceUtil;
import com.liferay.portal.service.OrganizationLocalServiceUtil;
import com.liferay.portal.service.PhoneLocalServiceUtil;
import com.liferay.portal.service.ServiceContext;
import com.liferay.portal.service.UserLocalServiceUtil;
import com.liferay.portal.service.WebsiteLocalServiceUtil;

public class RegistrationHelper {
	
	public static User createUser(long companyId, String emailAddress,
			String firstName, String lastName, boolean male,
			String mobileNumber, Date dateOfBirth, ServiceContext serviceContext) {
		
		boolean autoPassword = true;
		String password = StringPool.BLANK;
		boolean autoScreenName = true;
		String screenName = StringPool.BLANK;
		int facebookId = 0;
		String openId = StringPool.BLANK;
		String middleName = StringPool.BLANK;
		int prefixId = 0;
		int suffixId = 0;
		String jobTitle = StringPool.BLANK;
		long[] groupIds = null;
		long[] organizationIds = null;
		long[] roleIds = null;
		long[] userGroupIds = null;
		boolean sendEmail = true;
		
		long creatorUserId = 0l;
		try {
			creatorUserId = UserLocalServiceUtil.getDefaultUserId(companyId);
		} catch (PortalException e) {
			e.printStackTrace();
		} catch (SystemException e) {
			e.printStackTrace();
		}		
		
		Calendar dobCalendar = Calendar.getInstance();
		dobCalendar.setTime(dateOfBirth);
		
		User user = null;
		try {
			user = UserLocalServiceUtil.addUser(creatorUserId, companyId,
					autoPassword, password, password, autoScreenName, screenName,
					emailAddress, facebookId, openId, Locale.US, firstName,
					middleName, lastName, prefixId, suffixId, male, dobCalendar.get(Calendar.MONTH),
					dobCalendar.get(Calendar.DATE), dobCalendar.get(Calendar.YEAR), jobTitle, groupIds, organizationIds,
					roleIds, userGroupIds, sendEmail, serviceContext);
		} catch (PortalException e) {
			e.printStackTrace();
		} catch (SystemException e) {
			e.printStackTrace();
		}
		
		System.out.println("user successfully created...==> " + user);
		
		// inserting the mobile number for this user...
		if (Validator.isNotNull(mobileNumber)) {
			String iddCode = "91";
			int mobilePhoneTypeId = RegistrationHelper.getTypeId("mobile-phone", Contact.class.getName() + StringPool.PERIOD + "phone");			
			try {
				PhoneLocalServiceUtil.addPhone(user.getUserId(),
						Contact.class.getName(), user.getContactId(),
						mobileNumber, iddCode, mobilePhoneTypeId, true,
						serviceContext);
				System.out.println("phone successfully saved...");
			} catch (PortalException e) {
				e.printStackTrace();
			} catch (SystemException e) {
				e.printStackTrace();
			}
		}
		
		return user;
	}
	
	public static Organization createOrganization(long companyId, long userId,
			String organizationName, String website, String subdomain, ServiceContext serviceContext) {
		
		long organizationId = 0l;
		Organization organization = null;
		long parentOrganizationId = 0l;
		try {
			organization = OrganizationLocalServiceUtil.addOrganization(userId, parentOrganizationId, organizationName, false);
			organizationId = organization.getOrganizationId();
			System.out.println("organization successfully created...." + organization);
		} catch (PortalException e) {
			e.printStackTrace();
		} catch (SystemException e) {
			e.printStackTrace();
		}
		
		// WebSite
		if (Validator.isNotNull(website)) {
			int websiteTypeId = getTypeId("public", Organization.class.getName() + StringPool.PERIOD + "website");
			try {
				WebsiteLocalServiceUtil.addWebsite(userId,
						Organization.class.getName(), organizationId,
						IConstants.HTTP_PREFIX + website, websiteTypeId, true,
						serviceContext);
			} catch (PortalException e) {
				e.printStackTrace();
			} catch (SystemException e) {
				e.printStackTrace();
			}
		}
		
		// Attaching User to the Organization
		try {
			OrganizationLocalServiceUtil.addUserOrganization(userId, organizationId);
		} catch (SystemException e) {
			e.printStackTrace();
		}
		
		Indexer indexer = IndexerRegistryUtil.getIndexer(Organization.class);
		try {
			indexer.reindex(organization);
		} catch (SearchException e) {
			e.printStackTrace();
		}
		
		return organization;
	}
	
	private static int getTypeId(String name, String type) {
		
		int typeId = 0;
		
		try {
			List<ListType> types = ListTypeServiceUtil.getListTypes(type);
			
			for (ListType _type: types) {
				if (_type.getName().equalsIgnoreCase(name)) {
					typeId = _type.getListTypeId();
					break;
				}
			}
		} catch (SystemException e) {
			e.printStackTrace();
		}
		
		return typeId;
	}	
}